package cc.rengu.igas.cops.core.service.trans.unionpay;

import cc.rengu.igas.channel.upqc.facade.request.B2CATMCashNoticeRequest;
import cc.rengu.igas.channel.upqc.facade.response.B2CATMCashNoticeResponse;
import cc.rengu.igas.channel.upqc.facade.result.Result;
import cc.rengu.igas.cops.common.enums.RespCodeEnum;
import cc.rengu.igas.cops.facade.request.BankB2cATMCashNoticeRequest;
import cc.rengu.igas.cops.facade.response.BankB2cATMCashNoticeResponse;
import cc.rengu.oltp.service.base.BizBaseService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.UserOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.UserOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.service.common.enums.*;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.OrderInfo;
import cc.rengu.oltp.service.model.RiskMonitorInfo;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.OrderService;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.TransCustomImpl;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.service.realize.impl.OrderServiceImpl;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主扫  801322
 * ATM扫码取款 取现操作通知
 * 能力外放->收银台->银联
 *
 * @author fuyp
 * @version 1.0.0 创建时间：2020.10.20
 */
public class BankB2cATMCashNoticeService extends BizBaseService implements TransCustomImpl {

    @Override
    protected Object beforeBizProcess() throws Exception {
        BankB2cATMCashNoticeRequest bankB2cATMCashNoticeRequest = new BankB2cATMCashNoticeRequest();
        ConvertUtil.convertOutput(bankB2cATMCashNoticeRequest);
        return bankB2cATMCashNoticeRequest;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        // 录入参数
        BankB2cATMCashNoticeRequest bankB2cATMCashNoticeRequest = (BankB2cATMCashNoticeRequest) request;
        // 行号
        String instId = bankB2cATMCashNoticeRequest.getHeader().getInstId();
        //手机银行和直销银行可能不适用同一个银联机构号，通过渠道id区分
        String channelId = bankB2cATMCashNoticeRequest.getHeader().getChanlId();

        //step1：校验订单有效性
        UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
        UserOrderInfo userOrderInfo = userOrderInfoMapper.selectUserOrderInfoByOrderUrl(instId, bankB2cATMCashNoticeRequest.getTxnNo());

        if (null == userOrderInfo) {
            rglog.debug("查询用户订单表异常");
            throw new BizException(OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespCode(), OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespDesc());
        }

        /* 银联码，获取法人行在银联的机构号 */
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam unionPayCode = sysParamService.getSysParamInfo(instId, "OPMS_SYS_PARAM", "unionPayerOrgCode" + "_" + channelId);
        if (null == unionPayCode) {
            rglog.error("获取二维码服务失败！获取付款方法人行在银联的机构号失败！instId:{}", instId);
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        // 银联机构号
        String strUnonPayCode = unionPayCode.getParamValue();

        // 请求银联
        B2CATMCashNoticeRequest b2CATMCashNoticeRequest = new B2CATMCashNoticeRequest();
        BeanUtil.beanCopy(bankB2cATMCashNoticeRequest, b2CATMCashNoticeRequest);
        // 银联机构号
        b2CATMCashNoticeRequest.setIssInsCode(strUnonPayCode);
        b2CATMCashNoticeRequest.setTxnAmt(bankB2cATMCashNoticeRequest.getTxnAmt());
        b2CATMCashNoticeRequest.setCurrencyCode("156");

        cc.rengu.igas.channel.upqc.facade.base.Header upHeader = new cc.rengu.igas.channel.upqc.facade.base.Header();
        BeanUtil.beanCopy(bankB2cATMCashNoticeRequest.getHeader(), upHeader);
        upHeader.setTxnNum("0190000903");// 取款操作通知
        upHeader.setSrcSysId("OPMS");
        b2CATMCashNoticeRequest.setHeader(upHeader);
        DubboService dubboService = new DubboServiceImpl();
        Result<B2CATMCashNoticeResponse> responseResult = (Result<B2CATMCashNoticeResponse>) dubboService.callDubboService("upqcPayerService", "b2cATMCashNotice", b2CATMCashNoticeRequest);
        if (null == responseResult || !responseResult.isSuccess() || !"00000000".equals(responseResult.getResult().getRespCode())) {
            rglog.error("ATM取现操作通知失败,txnNo:<{}>", bankB2cATMCashNoticeRequest.getTxnNo(),
                    responseResult.getResult().getRespCode());
            throw new BizException(responseResult.getResult().getRespCode(), responseResult.getResult().getRespDesc());
        }

        //更新用户订单未终态
        UserOrderInfo orderInfo = new UserOrderInfo();
        BeanUtil.beanCopy(userOrderInfo, orderInfo);
        orderInfo.setOrderAmt(bankB2cATMCashNoticeRequest.getTxnAmt());
        orderInfo.setNeedPayAmt(bankB2cATMCashNoticeRequest.getTxnAmt());
        orderInfo.setRealPayAmt(bankB2cATMCashNoticeRequest.getTxnAmt());
        orderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
        orderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
        orderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
        orderInfo.setTxnNum(userOrderInfo.getTxnNum());

        //更新订单
        OrderService orderService = new OrderServiceImpl();
        if (!orderService.updateOrderStatus(orderInfo, null, null)) {
            rglog.error("更新失败");
        }

        BankB2cATMCashNoticeResponse bankB2cATMCashNoticeResponse = new BankB2cATMCashNoticeResponse();
        Map<String, String> respCode = new HashMap<>();
        respCode.put("respCode", responseResult.getResult().getRespCode());
        bankB2cATMCashNoticeResponse.setResp(respCode);
        bankB2cATMCashNoticeResponse.setRespCode(responseResult.getResult().getRespCode());
        bankB2cATMCashNoticeResponse.setRespDesc(responseResult.getResult().getRespDesc());
        bankB2cATMCashNoticeResponse.setPtlsh(DateUtil.getCurrentTime());
        bankB2cATMCashNoticeResponse.setPtrq(DateUtil.getCurrentDate());
        // 返回
        BizResponse bizResponse = new BizResponse();
        bizResponse.setResult(bankB2cATMCashNoticeResponse);// 返回结果
        bizResponse.setRspSysId(AppParamConstant.IGAS);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }


    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {
        BankB2cATMCashNoticeResponse retResponse = (BankB2cATMCashNoticeResponse) response.getResult();
        ConvertUtil.convertInput(retResponse);
    }

    @Override
    protected RiskMonitorInfo getRiskMonitorInfo() {
        return null;
    }

    @Override
    public ChannelCallCfg channelCallSceneDiscern(Object request, List<ChannelCallCfg> channelCallCfgList) throws Exception {
        return null;
    }

    @Override
    public CheckResultEnum channelCallPreCheck(Object request, ChannelCallCfg channelCallCfg, OrderInfo orderInfo) throws Exception {
        return null;
    }

    @Override
    public TransStatusEnum channelCallResultProc(Object request, ChannelCallCfg channelCallCfg, TransStatusEnum transStatusEnum, Object channelResponse, OrderInfo needUpdateOrderInfo) throws Exception {
        return null;
    }
}
